package com.kehutong.user.admin;

import com.kehutong.common.DoveClient;
import com.kehutong.common.util.Token;
import com.kehutong.contact.entity.Resident;
import com.kehutong.contact.entity.ResidentHouse;
import com.kehutong.contact.enums.BindType;
import com.kehutong.contact.enums.ResidentType;
import com.kehutong.contact.service.DataPvService;
import com.kehutong.contact.service.impl.ResidentServiceImpl;
import com.kehutong.user.service.AssetsUserService;
import com.kehutong.user.service.ParkingUserService;
import com.kehutong.user.service.ResidentUserService;
import com.kehutong.user.service.ShopUserService;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.logger.Logger;
import org.coraframework.logger.LoggerFactory;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.mvc.http.result.Result;
import org.coraframework.orm.jdbc.JdbcSession;
import org.coraframework.orm.jdbc.Page;
import org.coraframework.orm.jdbc.execute.entity.FindArray;
import org.coraframework.util.Objects;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 用户控制器
 * 前端账号体系对应的住户
 * 用户
 * 住户
 * 业主
 * 租客
 * 家属
 * 商户
 * 业主
 * 车户
 * 业主
 *
 * @author: liukunlong
 * @date: 2021-08-04 11:45
 */
@MvcAction
@ReqMapping("/admin/crm/assets/user")
public class AssetsUserController {

    static final Logger logger = LoggerFactory.getLogger(AssetsUserController.class);

    @Inject
    private ResidentServiceImpl residentService;


    @Inject
    private JdbcSession jdbcSession;

    @Inject
    private DoveClient doveClient;

    @Inject
    private DataPvService dataPvService;

    @Inject
    private ResidentUserService residentUserService;

    @Inject
    private ShopUserService shopUserService;

    @Inject
    private ParkingUserService parkingUserService;


    @Inject
    private AssetsUserService assetsUserService;



    @ReqMapping("/get")
    public Object get(Token token, JSONObject jsonObject) throws Exception {
        return assetsUserService.getUserFollow(jsonObject);
    }


    /**
     * 更新账号下面物业公司住户的启用禁用
     * @param jsonObject
     * @return
     * @throws Exception
     */
    @ReqMapping("/update")
    public Object update(JSONObject jsonObject) throws Exception {
        String id=jsonObject.getString("id");
        boolean enable=jsonObject.getBooleanValue("enable");
        Resident resident=jdbcSession.findById(Resident.class,id);
        resident.setEnable(enable);
        jdbcSession.updateById(resident);
        return Result.success(resident);
    }

    /**
     * 获取住户分页列表
     *
     * @param token
     * @param jsonObject
     * @return
     * @throws Exception
     */
    @ReqMapping("/community/page")
    public Object page(Token token, JSONObject jsonObject) throws Exception {
        Page<Map<String, Object>> page = assetsUserService.userCommunityPage(jsonObject);
        return Result.success(page);
    }


    @ReqMapping("/resident/user/page")
    public Page<Map<String, Object>> residentUserPage(JSONObject jsonObject) throws Exception {
        return residentUserService.residentUserPage(jsonObject);
    }

    @ReqMapping("/resident/user/export")
    public Object residentUserExport(JSONObject jsonObject) throws Exception {
        return residentUserService.residentUserExport(jsonObject);
    }

    @ReqMapping("/resident/user/checkFile")
    public Object residentUserCheckFile(JSONObject jsonObject) throws Exception {
        return residentUserService.checkFile(jsonObject);
    }

    @ReqMapping("/resident/user/readFile")
    public Object residentUserReadFile(Token token, JSONObject jsonObject) throws Exception {
        return residentUserService.readFile(token, jsonObject);
    }

    /**
     * 根据条件获取商铺业主
     *
     * @param jsonObject 查询对象
     * @return 操作结果
     * @throws Exception 操作异常
     */
    @ReqMapping("/resident/owner/page")
    public Page<ResidentHouse> residentOwnerPage(JSONObject jsonObject, Token token) throws Exception {
        return residentUserService.residentOwnerPage(jsonObject);
    }


    @ReqMapping("/shop/user/page")
    public Page<Map<String, Object>> shopUserPage(JSONObject jsonObject) throws Exception {
        return shopUserService.shopUserPage(jsonObject);
    }


    @ReqMapping("/shop/user/export")
    public Object shopUserExport(JSONObject jsonObject) throws Exception {
        return shopUserService.shopUserExport(jsonObject);
    }

    @ReqMapping("/shop/user/checkFile")
    public Object shopUserCheckFile(JSONObject jsonObject) throws Exception {
        return shopUserService.shopUserCheckFile(jsonObject);
    }

    @ReqMapping("/shop/user/readFile")
    public Object shopUserReadFile(Token token, JSONObject jsonObject) throws Exception {
        return shopUserService.shopUserReadFile(token, jsonObject);
    }
    /**
     * 根据条件获取商铺业主
     *
     * @param jsonObject 查询对象
     * @return 操作结果
     * @throws Exception 操作异常
     */
    @ReqMapping("/shop/owner/page")
    public Page<ResidentHouse> shopOwnerPage(JSONObject jsonObject, Token token) throws Exception {
        return shopUserService.shopOwnerPage(jsonObject);
    }


    @ReqMapping("/parking/user/page")
    public Page<Map<String, Object>> parkingUserPage(JSONObject jsonObject) throws Exception {
        return parkingUserService.parkingUserPage(jsonObject);
    }


    @ReqMapping("/parking/user/export")
    public Object parkingUserExport(JSONObject jsonObject) throws Exception {
        return parkingUserService.parkingUserExport(jsonObject);
    }

    @ReqMapping("/parking/user/checkFile")
    public Object parkingUserCheckFile(JSONObject jsonObject) throws Exception {
        return parkingUserService.parkingUserCheckFile(jsonObject);
    }

    @ReqMapping("/parking/user/readFile")
    public Object parkingUserReadFile(Token token, JSONObject jsonObject) throws Exception {
        return parkingUserService.parkingUserReadFile(token, jsonObject);
    }

    /**
     * 根据条件获取商铺业主
     *
     * @param jsonObject 查询对象
     * @return 操作结果
     * @throws Exception 操作异常
     */
    @ReqMapping("/parking/owner/page")
    public Page<ResidentHouse> parkingOwnerPage(JSONObject jsonObject, Token token) throws Exception {
        return parkingUserService.parkingOwnerPage(jsonObject);
    }

    /***
     * 根据住户标志删除住户信息
     * @param id 住户标志
     * @return 操作结果
     * @throws Exception 操作异常
     */
    @ReqMapping("/delete")
    public Object deleteResidentById(String id) throws Exception {
        Resident resident = jdbcSession.findOne(Resident.class)
                .eq("id",id)
                .eq("deleted", false).exe();
        if(Objects.isNull(resident)){
            return Result.error(1,"当前住户已经删除.");
        }

        List<String> bindTypeList = new ArrayList<>();
        bindTypeList.add(BindType.BIND.getId());
        FindArray<ResidentHouse> findResidentHouse = jdbcSession.findArray(ResidentHouse.class)
                .eq("residentNo",id)
                .in("bindType", bindTypeList)
                .eq("deleted", false)
                .order("createTime", "desc");
        List<ResidentHouse> residentHouseList = findResidentHouse.exe();
        if(Objects.nonEmpty(residentHouseList)){
            return Result.error(1,"当前住户存在已绑定资产.");
        }

        //删除业主
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("type",1);
        jsonObject.put("id",id);
        residentService.deleteResidentHouse(jsonObject);
        //删除家属及租客信息
        for(ResidentHouse residentHouse:residentHouseList){
            if(Objects.equal(ResidentType.FRIEND.getId(),residentHouse.getType().getId())){
                jsonObject.put("type",2);
                jsonObject.put("id",residentHouse.getId());
                residentService.deleteResidentHouse(jsonObject);
            }else if(Objects.equal(ResidentType.LESSEE.getId(),residentHouse.getType().getId())){
                jsonObject.put("type",3);
                jsonObject.put("id",residentHouse.getId());
                residentService.deleteResidentHouse(jsonObject);
            }
        }
        return Result.success();
    }


}
